<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 路径总和Ⅱ，找出所有的路径，所以是遍历一棵树，就无需返回值了 */
            var pathSum = function (root, targetSum) {
                let res = []
                //这里不需要直接把根节点的值作为初始值，因为深度优先遍历的时候会直接先把第一个节点添加进去，当然也可以不这么写
                function preOrder(root, path, sum) {
                    if (!root) return
                    path.push(root.val)
                    sum += root.val
                    if (!root.left && !root.right && sum == targetSum) {
                        return res.push([...path])
                    }
                    if (root.left) {
                        preOrder(root.left, path, sum)
                        path.pop()
                    }
                    if (root.right) {
                        preOrder(root.right, path, sum)
                        path.pop()
                    }
                }
                preOrder(root, [], 0)
                return res
            }
        </script>
    </body>
</html>
